package com.gmcc.docmanager.loader;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.gmcc.docmanager.entity.Application;
import com.gmcc.docmanager.entity.ApplicationList;
import com.gmcc.docmanager.entity.LibCodeType;

public class QueryApplicationResponseParser extends ResponseParser {

	public QueryApplicationResponseParser(ResponseListener listener) {
		super(listener);
	}

	@Override
	public void doParse(String url, HttpResponse httpresp, InputStream is,
			String postData) {
		super.doParse(url, httpresp, is, postData);
		if (netResult && rootElement != null) {
			int errorCode = 1;
			errorCode = stringToInt(getNodeTextValue("errorCode"));
			if (errorCode != 0) {
				response(createFailureResponse(errorCode));
			} else {
				ApplicationList applicationList = new ApplicationList();
				Node applicationsNode = getSingleNode("applications");
				if (applicationsNode != null) {
					NamedNodeMap nodeMap = applicationsNode.getAttributes();
					String unitID = getAttributeValue(nodeMap, "unitID");
					int pageNum = stringToInt(getAttributeValue(nodeMap,
							"pageNum"));
					int page = stringToInt(getAttributeValue(nodeMap, "page"));
					int totalCount = stringToInt(getAttributeValue(nodeMap,
							"totalCount"));
					applicationList.setUnitID(unitID);
					applicationList.setPageNum(pageNum);
					applicationList.setPage(page);
					applicationList.setTotalCount(totalCount);

					List<Application> applications = new ArrayList<Application>();
					NodeList applicationNodeList = rootElement
							.getElementsByTagName("application");
					if (applicationNodeList != null) {
						for (int i = 0; i < applicationNodeList.getLength(); i++) {
							Node applicationNode = applicationNodeList.item(i);
							NamedNodeMap documentNodeMap = applicationNode
									.getAttributes();
							Application application = new Application();
							application.setDocID(getAttributeValue(
									documentNodeMap, "docID"));
							application.setEfileID(getAttributeValue(
									documentNodeMap, "efileID"));
							application.setLibCode(LibCodeType
									.valueOf(stringToInt(getAttributeValue(
											documentNodeMap, "libCode"))));
							application.setRn(stringToInt(getAttributeValue(
									documentNodeMap, "rn")));

							for (Node applicationChildNode = applicationNode
									.getFirstChild(); applicationChildNode != null; applicationChildNode = applicationChildNode
									.getNextSibling()) {
								if (applicationChildNode.getNodeType() == Node.ELEMENT_NODE) {
									if ("applyDate".equals(applicationChildNode
											.getNodeName())) {
										application
												.setApplyDate(getNodeTextValue(applicationChildNode));
									} else if ("result"
											.equals(applicationChildNode
													.getNodeName())) {
										application
												.setResult(stringToInt(getNodeTextValue(applicationChildNode)));
									} else if ("approver"
											.equals(applicationChildNode
													.getNodeName())) {
										application
												.setApprover(getNodeTextValue(applicationChildNode));
									} else if ("filename"
											.equals(applicationChildNode
													.getNodeName())) {
										application
												.setFilename(getNodeTextValue(applicationChildNode));
									} else if ("reason"
											.equals(applicationChildNode
													.getNodeName())) {
										application
												.setReason(getNodeTextValue(applicationChildNode));
									}
								}
							}
							applications.add(application);
						}
					}
					applicationList.setApplications(applications);
				}

				Response response = new Response();
				response.setCode(errorCode);
				response.setResponseObject(applicationList);
				response(response);
			}
		}
	}
}
